package cn.zyx.sys.web;

import cn.zyx.common.GlobalAjaxJson;
import cn.zyx.sys.entity.SysConfig;
import cn.zyx.sys.entity.vo.SysConfigVo;
import cn.zyx.sys.service.SysConfigService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


/**
 * 系统配置Controller
 * @author zyx
 */
@RestController
@RequestMapping("/sys/sysConfig")
public class SysConfigController {

	@Autowired
	private SysConfigService sysConfigService;


	/**
	 * 系统配置
	 */
	@GetMapping("queryById")
	public GlobalAjaxJson queryById() {
		SysConfig config = sysConfigService.get("1");
		return GlobalAjaxJson.success().put("config", config);
	}

	/**
	 * 系统配置
	 */
	@GetMapping("getConfig")
	public GlobalAjaxJson getConfig() {
		SysConfig config = sysConfigService.get("1");
		SysConfigVo vo = new SysConfigVo();
		vo.setDefaultLayout (config.getDefaultLayout ());
		vo.setDefaultTheme(config.getDefaultTheme ());
		vo.setLogo (config.getLogo ());
		vo.setProductName (config.getProductName ());
		return GlobalAjaxJson.success().put("config", vo);
	}

	@GetMapping("testSms")
	public GlobalAjaxJson testSms(@RequestParam("tel")String tel) {
		/*SendSmsResponse response =  SmsUtils.sendSms(tel,"{code:123}");
		if (response.getCode() != null && response.getCode().equals("OK")) {
			return GlobalAjaxJson.success("短信发送成功!");
		}else {
			return GlobalAjaxJson.error("短信发送失败!"+ response.getMessage());
		}*/
		return null;
	}

	/**
	 * 保存系统配置
	 */
	@PostMapping("save")
	public GlobalAjaxJson save(SysConfig config)throws Exception {
		if(config.getMultiAccountLogin() == null){
			config.setMultiAccountLogin("0");
		}
		SysConfig target = sysConfigService.get("1");
		// 设置属性 -- 保存配置
		// MyBeanUtils.copyBeanNotNull2Bean(config, target);
		// target.setIsNewRecord(false);
		sysConfigService.save(target);
		return GlobalAjaxJson.success("保存系统配置成功").put("config", target);
	}

}
